<?php
/**
 * @desc 产品分类管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_protype extends ctrl_base {
    private static $dirName = 'protype';
    
    /**
     * @desc 产品大分类列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('produce_big_class'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$class_name = isset($_POST['class_name']) ? trim($_POST['class_name']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($class_name)) {
    		$condition['class_name'] = $class_name;
    	}
		//订单获取
    	$produce_list = model_protype :: get_big_class_list($nextpage,$pagesize,$condition);
    	$total = array_pop($produce_list);
		//模板赋值
    	self::smarty() -> assign('produce_list', $produce_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('class_name', $class_name);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/big_class.html');    	
    }
	//添加大分类
	public static function big_class_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('big_class_add'));
		if($_POST){

			$produce = self::get_request_data();
			if(model_protype::get_class_by_name($produce['class_name'])){
				self::ajax_ouput(300, '该分类已存在');
			}
			$produce['editer'] = $_SESSION['admin']['username'];
			$produce['author'] = $_SESSION['admin']['username'];

			$result = model_protype::big_class_add($produce);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','produce_class','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		self::smarty() -> display(self::$dirName . '/big_class_add.html');
	}
	//删除订单
	public static function big_class_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('big_class_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$id[] = $v;
			$info = model_protype::get_class_by_id($v);
			/*if(!empty($info) && $info['status'] == 0){
				self::ajax_ouput(300, "分类 {$info['class_name']} 为有效状态，不可删除");
			}*/
			$child = model_protype::get_child_by_id($v);
			if(!empty($child)){
				self::ajax_ouput(300, "分类 {$info['class_name']} 存在子分类，不可删除");
			}
		}
    	$data = array('delete_flag' => 1);

    	$st = model_protype::update_produce_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','produce_class');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑大分类
	 public static function big_class_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$produce = model_protype::get_class_by_id($id);
		$ctrl_tag = 'big_class_edit_other';
		if($produce['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'big_class_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$produce = array();

			$produce = self::get_request_data();
			unset($produce['id']);

			if(model_protype::get_class_by_name($produce['class_name'],$id)){
				self::ajax_ouput(300, '该分类已存在');
			}
			$info = model_protype::get_class_by_id($id);
			$child = model_protype::get_child_by_id($id);
			if($produce['status'] == 1 && !empty($child)){
				self::ajax_ouput(300, "分类 {$info['class_name']} 存在子分类，不可设置为无效");
			}
			$produce['editer'] = $_SESSION['admin']['username'];

			$result = model_protype::update_produce_info(explode(',',$id), $produce);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','produce_class','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('produce', $produce);
    	self::smarty() -> display(self::$dirName . '/big_class_edit.html');
    }

}
